package com.wf.springboot.server.handler;

/**
 * @author: wf
 * @created: 2024/6/23 19:26
 */

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.wf.springboot.common.exception.BusinessException;
import com.wf.springboot.common.result.Result;
import com.wf.springboot.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 全局异常拦截
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 没有登录
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public Result notLoginException(NotLoginException e) {
        log.error("登录异常执行了，失败原因：{}", e.getMessage());
        return Result.fail(ResultCodeEnum.UN_AUTHORIZED);
    }

    /**
     * 没有权限
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NotPermissionException.class)
    public Result notPermissionException(NotPermissionException e) {
        log.error("没有权限异常执行了，失败原因：{}", e.getMessage());
        return Result.fail(ResultCodeEnum.NO_PERMISSION);
    }

    /**
     * 没有角色
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NotRoleException.class)
    public Result notRoleException(NotRoleException e) {
        log.error("没有角色异常执行了，失败原因：{}", e.getMessage());
        return Result.fail(ResultCodeEnum.NO_PERMISSION);
    }

    /**
     * 请求错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("请求方式异常执行了，失败原因：{}", e.getMessage());
        return Result.fail(ResultCodeEnum.REQUEST_ERROR);
    }

    /**
     * jsr参数校验拦截
     *
     * @param e
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("数据校验出现问题{}，异常类型为{}", e.getMessage(), e.getClass());
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
        Result errorResult = Result.fail(message);
        return errorResult;
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result businessException(BusinessException e) {
        String message = e.getMessage();
        log.error("业务异常执行了，失败原因：{}", message);
        return Result.fail(message);
    }

    /**
     * 未知异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        log.error("未知异常，报错原因：{}", e.getMessage());
        return Result.fail();
    }

}
